<?php

class Picture {
    
    public static function getCleanPics() {
        
        global $db;
        
        $table = 'photographs';
        $db->select($table);
        $pics = $db->fetchAll();

        return $pics;
    }
    
    
    public static function getAlbums() {
        
        global $db;
        
//        $table = 'albums';
//        $db->select($table, '*', ' 1 ORDER BY name ASC', array(), true);
//        $albums = $db->fetchAll();
        
        $db->query('SELECT * , COUNT( photographs.id ) AS images_count
                        FROM  `albums`
                        JOIN photographs
                        ON albums.id = photographs.album_id
                        GROUP BY albums.id
                        ORDER BY albums.name');
        
        $albums = $db->fetchAll();

        return $albums;
    }
    
    public static function getAlbumId($album) {
        
        global $db;
        
        $where_data = array('name' => $album);
        $db->select('albums', 'id', 'name = :name', $where_data);
        $data = $db->fetch();
        $albumId = $data['id'];
        
        return $albumId;
    }
    
    public static function getAlbumName($albumId) {
        
        global $db;
        
        $where_data = array('id' => $albumId);
        $db->select('albums', 'name', 'id = :id', $where_data);
        $data = $db->fetch();
        $albumName = $data['name'];
        
        return $albumName;
    }
    
    public static function getAlbumContent($albumId) {
        
        global $db;
        
        $where_data = array('id' => $albumId);
        $db->select('albums', 'content', 'id = :id', $where_data);
        $data = $db->fetch();
        $albumContent = $data['content'];
        
        return $albumContent;
    }
    
    public static function getDefaultAlbum() {
        
        global $db;
        
        $db->select('albums', '*', '1 ORDER BY `id` DESC');
        $album = $db->fetch(); 
        
        return $album;
    }
    
    public static function getAlt($album_id) {
        
        global $db;
        
        $where_data = array('id' => $album_id);
        $db->select('albums', 'alt', 'id = :id', $where_data);
        $data = $db->fetch();
        
        return $data['alt'];
    }
    
    public static function getDefaultPhoto() {
       
        global $db;
        
        $defaultAlbum = self::getDefaultAlbum();
        $where_data = array('album_id' => $defaultAlbum['id']);
        $db->select('photographs', '*', ' album_id = :album_id', $where_data);
        $picture = $db->fetch();
        
        return $picture;
    }
    
    public static function getPhotoFromAlbum($album, $picture = null) {
        
        if($picture == null) {
            self::getFirstPhoto($album);
            return;
        }
        
        $album_id = self::getAlbumId($album);
        $where_data = array('album_id' => $album_id, 'id' => $picture);
        $db->select('photographs', '*', 'id = :id AND album_id = :album_id', $where_data);
        $photo = $db->fetch();
        
        return $photo;
    }
    
    public static function getFirstPhoto($album) {
        
        global $db;
        
        $album_id = self::getAlbumId($album);
        $where_data = array('album_id' => $album_id);
        $db->select('photographs', '*', 'album_id = :album_id ORDER BY `id`', $where_data);
        $photo = $db->fetch();
        
        return $photo;
    }
    
    public static function currentPhoto($album = null, $picture = null) {
        
        global $db;
        $data = array();
        
        if($album == null) {
            $photo = self::getDefaultPhoto();
            $album = self::getAlbumName($photo['album_id']);
        }
        else if($picture == null) {
            $photo = self::getFirstPhoto($album);
            $album = self::getAlbumName($photo['album_id']);
            
        }
        else {
            $album_id = self::getAlbumId($album);
            $where_data = array('id' => $picture, 'album_id' => $album_id);
            $db->select('photographs', '*', 'id = :id AND album_id = :album_id', $where_data);
            $photo = $db->fetch();
        }
        
        
        $data['id'] = $photo['id'];
        $data['album_id'] = $photo['album_id'];
        $data['fb'] = ROOT . 'index.php?album=' . $album . '&image=' . $photo['id'];
        $data['image'] = ALBUM_PATH . $album . '/' . $photo['source'];
        $data['title'] = $photo['title'];
        $data['alt'] = self::getAlt($photo['album_id']);
        
        $db->select('photographs', '*', 'album_id = :album_id', array('album_id' => $photo['album_id']));
        $allData = $db->fetchAll();//pre($allData);
        
        foreach($allData as $k => $v) {
            if($v['id'] == $data['id']) {
                $data['position'] = $k + 1;
                break;
            }
        }
        
        return $data;
    }
    
    public static function hasPrevious($photo) {
        
        global $db;
        
        
        $id = $photo['id'];
        $album_id = $photo['album_id'];
        
        $where_data = array('album_id' => $album_id);
        $db->select('photographs', '*', 'album_id = :album_id', $where_data);
        $photos = $db->fetchAll();
        $photos = indexOf('id', $photos);
        
        $photo = array();
        end($photos);
        while(current($photos)) {
            if($id == key($photos)) {
                $prev = prev($photos);
                if($prev != false) {
                    $photo = $prev;
                }
                else {
                    $photo = false;
                }
            }
            prev($photos);
        }

        if($photo == false) {
            return '#';
        }
        
        $album = self::getAlbumName($photo['album_id']);
//        $path = ROOT . $album . '/' . $photo['id'];
        $path = 'index.php?album=' . $album . '&image=' . $photo['id'];
        

        return $path;
    }
    
    public static function hasNext($photo) {
        
        global $db;
        
        
        $id = $photo['id'];
        $album_id = $photo['album_id'];
        
        $where_data = array('album_id' => $album_id);
        $db->select('photographs', '*', 'album_id = :album_id', $where_data);
        $photos = $db->fetchAll();
        $photos = indexOf('id', $photos);
        
        $photo = array();
        reset($photos);
        while(current($photos)) {
            if($id == key($photos)) {
                $next = next($photos);
                if($next != false) {
                    $photo = $next;
                }
                else {
                    $photo = false;
                }
            }
            next($photos);
        }
        
        if($photo == false) {
            
            return '#';
        }
        
        $album = self::getAlbumName($photo['album_id']);
//        $path = ROOT . $album . '/' . $photo['id'];
        $path = 'index.php?album=' . $album . '&image=' . $photo['id'];
        
        
        return $path;
    }
    
    
    public static function countPicsInAlbum($album) {
        
        if($album == null) {
            return false;
        }
        
        global $db;
        
        $albumId = self::getAlbumId($album);

        $db->select('photographs', '*', 'album_id = :album_id', array('album_id' => $albumId));
        $db->fetchAll();
        
        return $db->rowCount();
    }
    
    public static function getCounterValue() {
        global $db;
        
        $db->select('shmekeri', '*');
        $defaultCount = $db->fetch();
        
        return $defaultCount['count'];
    }
    
}

